package com.example.actordemo1.io;

import akka.actor.*;
import akka.io.Tcp;
import akka.io.TcpManager;
import akka.io.TcpMessage;
import akka.util.ByteString;

import java.net.InetSocketAddress;

public class IoTcpActor  extends AbstractActor {



    @Override
    public void preStart() throws Exception {
        System.out.println("开始");
        ActorRef manager = Tcp.get(getContext().system()).manager();
//        Tcp.Command bind = TcpMessage.bind(getSelf(), new InetSocketAddress("127.0.0.1", 1234), 100);
        manager.tell(TcpMessage.bind(getSelf(),new InetSocketAddress("127.0.0.1",1234),100),getSelf());
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder().match(Tcp.Bound.class,msg->{
            System.out.println("绑定");
            Tcp.Bound bound = msg;
        }).match(Tcp.Connect.class,msg->{
            System.out.println("con");
            Tcp.Connect connect = msg;
            System.out.println("conn"+connect);
            ActorRef actorRef = getContext().actorOf(Props.create(ServerHandler.class));
            getSender().tell(TcpMessage.register(actorRef),getSelf());
        }).build();
    }

    public static void main(String[] args) {
        ActorSystem actorSystem = ActorSystem.create("sys");
//        ActorRef actorRef = actorSystem.actorOf(Props.create(IoTcpActor.class), "IoTcpActor");

//        actorRef.tell("111",ActorRef.noSender());
    }
}


class ServerHandler extends AbstractActor {


    @Override
    public Receive createReceive() {
        return receiveBuilder().match(Tcp.Received.class,msg->{
            Tcp.Received received =  msg;
            ByteString data = received.data();
            String con = data.utf8String();
            System.out.println("serve"+con);
            getSender().tell(TcpMessage.write(ByteString.fromString("Thank")),getSelf());

        }).match(Tcp.ConnectionClosed.class,msg->{
            System.out.println("Connection is closed "+msg);
            getContext().stop(getSelf ()) ;
        }).matchAny(msg->{
            System.out.println("any"+msg);
        }).build();
    }

    class ClientActor extends AbstractActor {

        @Override
        public void preStart() throws Exception {
            System.out.println("客户开始");
            ActorRef manager = Tcp.get(getContext().system()).manager();
            manager.tell(TcpMessage.connect(new InetSocketAddress("127.0.0.1",1234)),getSelf());
        }

        @Override
        public Receive createReceive() {
            return receiveBuilder().match(Tcp.Connect.class,msg->{
                ActorRef actorRef = getContext().actorOf(Props.create(ClientActor.class), "clientActor");
                getSender().tell(TcpMessage.register(actorRef),getSelf());
                getSender().tell(TcpMessage.write(ByteString.fromString("HelloAkka")),getSelf());
            }).build();
        }
    }

    public static void main(String[] args) {
        ActorSystem actorSystem = ActorSystem.create("sys");
        ActorRef actorRef = actorSystem.actorOf(Props.create(IoTcpActor.class), "IoTcpActor");
//        ActorRef client = actorSystem.actorOf(Props.create(ClientActor.class), "clientActor");
    }
}